package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.BaiVietDao;
import com.tt.fashionWebSite.dao.DanhMucBaiVietDao;
import com.tt.fashionWebSite.domain.BaiViet;
import com.tt.fashionWebSite.domain.DanhMucBaiViet;
import com.tt.fashionWebSite.domain.KichThuoc;
import com.tt.fashionWebSite.domain.LoaiDanhMuc;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_PostService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_postService")
public class Ad_PostServiceImp extends GenericService implements Ad_PostService {
    @Autowired
    @Qualifier("baiViet")
    private BaiVietDao baiVietDao;

    @Autowired
    @Qualifier("danhMucBaiViet")
    private DanhMucBaiVietDao danhMucBaiVietDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminPost(BaiVietDto postDto) {
        BaiViet baiViet = baiVietDao.findById(postDto.getMaBV());
        if(baiViet == null){
            baiViet = new BaiViet();
            baiViet.setMaBV(postDto.getMaBV());
            DanhMucBaiViet danhMucBaiViet = danhMucBaiVietDao.findById(postDto.getDanhMucBaiViet().getMaDMBV());
            baiViet.setDanhMucBaiViet(danhMucBaiViet);
            baiViet.setHinhAnh(postDto.getHinhAnh());
            baiViet.setNgayViet(postDto.getNgayViet());
            baiViet.setNoiDung(postDto.getNoiDung());
            baiViet.setTieuDe(postDto.getTieuDe());
            baiViet.setTomTat(postDto.getTomTat());
            baiVietDao.insert(baiViet);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    @Override
    public List<DanhMucBaiVietDto> getPostCategory() {
        return mapper.mapCollection(danhMucBaiVietDao.getPostCategory(), DanhMucBaiVietDto.class);
    }

    @Override
    public List<BaiVietDto> getAllPost() {
        return mapper.mapCollection(baiVietDao.getAllPost(), BaiVietDto.class);
    }

    public BaiVietDto getPostInfoEdit(BaiVietDto postDto){
        BaiViet baiViet = baiVietDao.findById(postDto.getMaBV());
        BaiVietDto baiVietDto = mapper.map(baiViet, BaiVietDto.class,BAI_VIET_MAP_ID);
        return  baiVietDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public BaiVietDto registerPostEdit(BaiVietDto postDto) {
        BaiViet baiViet = baiVietDao.findById(postDto.getMaBV());
        try {
            baiViet.setMaBV(postDto.getMaBV());
            baiViet.setTomTat(postDto.getTomTat());
            baiViet.setTieuDe(postDto.getTieuDe());
            baiViet.setNoiDung(postDto.getNoiDung());
            baiViet.setHinhAnh(postDto.getHinhAnh());
            baiViet.setNgayViet(postDto.getNgayViet());
            DanhMucBaiViet danhMucBaiViet = danhMucBaiVietDao.findById(postDto.getDanhMucBaiViet().getMaDMBV());
            baiViet.setDanhMucBaiViet(danhMucBaiViet);
            baiVietDao.update(baiViet);
            return postDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public List<DanhMucBaiVietDto> getAllCategory() {
        return mapper.mapCollection(danhMucBaiVietDao.getPostCategory(), DanhMucBaiVietDto.class);
    }

}

